package com.wfh.activity;

import java.util.ArrayList;

import com.wfh.R;
import com.wfh.adapter.TicketsAdapter;
import com.wfh.bean.TicketsBean;
import com.wfh.util.MyDialog;
import com.wfh.util.Util;

import android.os.Bundle;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;

public class TicketsActivity extends CommonActivity {
	private MyDialog dialog;
	private MyDialog alert;
	private ArrayList<TicketsBean> list;
	private TicketsAdapter adapter;
	private ListView listview;
	private boolean mbundle = true;
	private BroadcastReceiver datareceiver;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState, R.layout.activity_tickets);

		listview = (ListView) findViewById(R.id.listView1);
		dialog = MyDialog.create(this);

		// 注册recevier广播接收器
		IntentFilter filter = new IntentFilter();
		filter.addAction(Util.D_TICKETS);
		filter.addAction(Util.D_QTICKETS);
		datareceiver = new DataReceiver();
		registerReceiver(datareceiver, filter);
		
		dialog.show();
		HomeActivity.service.getTickets(this, null);
	}

	// 接收器
	private class DataReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			Bundle bd = intent.getExtras();
			if (intent.getAction().equals(Util.D_TICKETS)) {
				list = bd.getParcelableArrayList("tickets");
				if (list == null || list.size() == 0)
					return;
				if (adapter != null) {
					adapter.list = list;
					adapter.notifyDataSetChanged();
//					Util.setListViewHeightBasedOnChildren(listview);
					dialog.hide();
					mbundle = true;
					return;
				}
				adapter = new TicketsAdapter(TicketsActivity.this, list);
				listview.setAdapter(adapter);
//				Util.setListViewHeightBasedOnChildren(listview);
				dialog.hide();
				mbundle = true;
			}else if(intent.getAction().equals(Util.D_QTICKETS)){
				int ret=bd.getInt("ret");
				String status=bd.getString("status");
				if(ret==101){
					createDialog();
				}
				if(ret==1){
					Util.startactivity(TicketsActivity.this, TicketsActivity.class);
				}
				dialog.hide();
				Util.showToast(TicketsActivity.this, status);
			}
		}
	}

	public void createDialog(){
		if(alert!=null){
			alert.show();
			return;
		}
		View view;
		LayoutInflater minflater = LayoutInflater
				.from(TicketsActivity.this);
		view = minflater.inflate(R.layout.tickets_dialog2, null);
		Button btn = (Button) view.findViewById(R.id.button1);
		// 注册事件
		btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Util.startactivity(TicketsActivity.this,
						LoginActivity.class);
				alert.hide();
			}
		});
		alert = MyDialog.create(TicketsActivity.this, view);
		alert.show();
		
	}
	
	
	@Override
	protected void onDestroy() {
		if (dialog != null)
			dialog.cancel();
		if(alert!=null) alert.cancel();
		unregisterReceiver(datareceiver);
		super.onDestroy();
	}

}
